package com.kingdee.pda.ui.popupwindow;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.kingdee.pda.R;
import com.kingdee.pda.adapter.AbsBaseAdapter;
import com.kingdee.pda.entity.InvstatusBean;
import com.kingdee.pda.entity.LocationListBean;
import com.kingdee.pda.entity.ReplenishOrderBean;
import com.kingdee.pda.entity.Warehouse;
import com.kingdee.pda.entity.WarehouseLocation;
import com.kingdee.pda.ui.goods_putaway.OwnerBean;
import com.kingdee.pda.ui.scan_inwarehouse.ScanReceiveOrderBean;
import com.kingdee.pda.ui.scan_inwarehouse.ScanReceiveShowDetailBean;
import com.kingdee.pda.ui.sn_query_edit.SnStatus;
import com.kingdee.pda.util.ViewHolder;

import java.util.List;

/**
 * 列表选择悬浮框
 * Created by zhanlg
 * DATE 2019/7/16
 * TIME 18:07
 */

public class ListSelectorPpWindow<T> extends PopupWindow {


    private Context mContext;
    private ListView mListView;
    private List<T> mData;
    private int mWindowWidth;
    private int mBgResId = 0;
    private onItemSelectedListener mListener;
    public ListAdapte adapter;


    public ListSelectorPpWindow(Context context, int width, List<T> data, onItemSelectedListener listener) {
        super(context);
        this.mContext = context;
        this.mWindowWidth = width;
        this.mData = data;
        this.mListener = listener;

        initLayout();
    }

    public ListSelectorPpWindow(Context context, int bgResId, int width, List<T> data, onItemSelectedListener listener) {
        super(context);
        this.mContext = context;
        this.mBgResId = bgResId;
        this.mWindowWidth = width;
        this.mData = data;
        this.mListener = listener;

        initLayout();
    }

    private void initLayout() {
        setWidth(mWindowWidth);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        View contentView = LayoutInflater.from(mContext).inflate(R.layout.popupwindow_list_selector, null);
//        setBackgroundDrawable(mContext.getResources().getDrawable(R.mipmap.popup_margin_shade_centre));
        setBackgroundDrawable(mContext.getResources().getDrawable(mBgResId == 0 ? R.drawable.shape_bg_white_rc : mBgResId));
        setContentView(contentView);
        setOutsideTouchable(true);

        initView(contentView);
    }

    private void initView(View view) {
        mListView = view.findViewById(R.id.listview);
        adapter = new ListAdapte(mContext);
        adapter.setData(mData);
        mListView.setAdapter(adapter);

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (mListener != null) {
                    dismiss();
                    mListener.onItemSelected(mData.get(position));
                }
            }
        });
    }


    public class ListAdapte extends AbsBaseAdapter<T> {

        public ListAdapte(Context context) {
            super(context);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(context).inflate(R.layout.layout_pw_item_selector, null);
            }

            TextView tv_item = ViewHolder.get(convertView, R.id.tv_item);
            final T t = getData().get(position);
            if (t instanceof String) {
                String content = ((String) t);
                tv_item.setText(content);
            } else if (t instanceof WarehouseLocation) {
                WarehouseLocation warehouseLocation = ((WarehouseLocation) t);
                tv_item.setText(warehouseLocation.getLocationName());
            } else if (t instanceof ReplenishOrderBean.DataBean) {
                ReplenishOrderBean.DataBean dataBean = ((ReplenishOrderBean.DataBean) t);
                tv_item.setText(dataBean.getBillNo());
            } else if (t instanceof Warehouse) {
                Warehouse dataBean = ((Warehouse) t);
                tv_item.setText(dataBean.getName());
            } else if (t instanceof ScanReceiveShowDetailBean) {
                ScanReceiveShowDetailBean dataBean = ((ScanReceiveShowDetailBean) t);
                tv_item.setText(dataBean.getOwnername());
            } else if (t instanceof SnStatus) {
                SnStatus dataBean = ((SnStatus) t);
                tv_item.setText(dataBean.getDes());
            } else if (t instanceof LocationListBean) {
                LocationListBean dataBean = ((LocationListBean) t);
                tv_item.setText(dataBean.getInvStatusName());
            }else if (t instanceof OwnerBean) {
                OwnerBean dataBean = ((OwnerBean) t);
                tv_item.setText(dataBean.getOwnerName());
            } else if(t instanceof InvstatusBean) {
                InvstatusBean bean = ((InvstatusBean) t);
                tv_item.setText(bean.name);
            }
            return convertView;
        }
    }

    public interface onItemSelectedListener {
        void onItemSelected(Object item);
    }
}
